home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / comm / rzsz0916.zip / MINIRB.C < prev    next >
C/C++ Source or Header  |  1995-01-14  |  3KB  |  86 lines

  1. char* Version= "minirb 3.02 12-21-94 Copyright 1994 Omen Technology INC";
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <setjmp.h>
  5.  
  6. FILE *fout; long Bytesleft; int Blklen; char secbuf[1024]; char linbuf[1024];
  7. int Lleft=0; jmp_buf tohere;
  8.  
  9. void mode(n) { if (n) system("stty raw -echo"); else system("stty echo -raw"); }
  10.  
  11. void alrm(c) { longjmp(tohere, -1); }
  12.  
  13. void bibi(n) {
  14.   mode(0); fprintf(stderr, "minirb: signal %d; exiting", n); exit(128+n); }
  15.  
  16. main() {
  17.  mode(1); if (signal(SIGINT, bibi) == SIG_IGN) {
  18.   signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN); } else {
  19.   signal(SIGINT, bibi); signal(SIGKILL, bibi); }
  20.  printf("%s\r\n\n\n", Version);
  21.  printf("Send your files with a YAM/ZCOMM \042sb file ...\042 command\r\n");
  22.  wcreceive(); mode(0); exit(0); }
  23.  
  24. wcreceive() {
  25.  for (;;) {
  26.   if (wcrxpn(secbuf) == -1) break;
  27.   if (secbuf[0]==0) return;
  28.   if (procheader(secbuf)== -1 || wcrx()== -1) break; } }
  29.  
  30. wcrxpn(rpn) char *rpn; { register c;
  31. et_tu:
  32.  sendline(025); Lleft=0; while ((c = wcgetsec(rpn)) != 0) {
  33.   if (c == -10) { sendline(6); Lleft=0; rdln(2); goto et_tu; } return -1; }
  34.  sendline(6); return 0; }
  35.  
  36. wcrx() { register int sectnum, sectcurr, sendchar, cblklen;
  37.  sectnum=0; sendchar=025;
  38. for (;;) {
  39.   sendline(sendchar); Lleft=0; sectcurr=wcgetsec(secbuf);
  40.   if (sectcurr==(sectnum+1 & 0377)) {
  41.    sectnum++; cblklen = Bytesleft>Blklen ? Blklen:Bytesleft;
  42.    fwrite(secbuf, cblklen, 1, fout);
  43.    if ((Bytesleft-=cblklen) < 0) Bytesleft = 0;
  44.    sendchar=6; }
  45.   else if (sectcurr==(sectnum&0377)) sendchar=6;
  46.   else if (sectcurr== -10) { fclose(fout); sendline(6); Lleft=0; return 0; }
  47.   else return -1; } }
  48.  
  49. wcgetsec(rxbuf) char *rxbuf; {
  50.  register checksum, wcj, firstch; register char *p; int sectcurr, errors;
  51.  for (errors=0; errors<15; errors++) {
  52.   if ((firstch=rdln(5))==2) { Blklen=1024; goto get2; }
  53.   if (firstch==1) { Blklen=128;
  54. get2:
  55.    sectcurr=rdln(2); checksum=0;
  56.    if ((sectcurr+(rdln(2)))==0377) {
  57.     for (p=rxbuf,wcj=Blklen; --wcj>=0; ) {
  58.      if ((firstch=rdln(2)) < 0) goto bilge;
  59.      checksum += (*p++ = firstch); }
  60.     if ((firstch=rdln(2)) < 0) goto bilge;
  61.     if (((checksum-firstch)&0377)==0) return sectcurr;
  62.    } }
  63.   else if (firstch==4) return -10;
  64.   else if (firstch==24) return -1;
  65. bilge: while(rdln(2)!= -2) ;
  66.   sendline(025); Lleft=0; }
  67.  return -1; }
  68.  
  69. rdln(timeout) int timeout; { static char *cdq;
  70.  if (--Lleft >= 0) return (*cdq++ & 0377);
  71.  if (setjmp(tohere)) { Lleft = 0; return -2; }
  72.  signal(SIGALRM, alrm); alarm(timeout);
  73.  Lleft=read(0, cdq=linbuf, 1024); alarm(0);
  74.  if (Lleft < 1) return -2;
  75.  --Lleft; return (*cdq++ & 0377); }
  76.  
  77. procheader(name) char *name; { register char *p;
  78.  Bytesleft = 2000000000L; p = name + 1 + strlen(name);
  79.  if (*p) sscanf(p, "%ld", &Bytesleft);
  80.  if ((fout=fopen(name, "w")) == NULL) return -1;
  81.  return 0; }
  82.  
  83. sendline(c) { char d; d = c; write(1, &d, 1); }
  84.  
  85. /* End of minirb.c */
  86.